home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / mesa-amiwin / samples / speed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-03  |  8.6 KB  |  422 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #define _HPUX_SOURCE
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <stdlib.h>
  31. #include "gltk.h"
  32.  
  33. #ifdef __unix
  34. #include <sys/times.h>
  35. #include <sys/param.h>
  36. #endif
  37.  
  38.  
  39. #define GAP 10
  40. #define ROWS 1
  41. #define COLS 4
  42.  
  43.  
  44. GLenum rgb, doubleBuffer, directRender, windType;
  45. GLint windW, windH;
  46.  
  47. GLint boxW, boxH;
  48.  
  49. GLenum antialiasing = GL_FALSE;
  50. GLenum depthTesting = GL_FALSE;
  51. GLenum fogging = GL_FALSE, niceFogging = GL_FALSE;
  52. GLenum lighting = GL_FALSE;
  53. GLenum shading = GL_FALSE;
  54. GLenum texturing = GL_FALSE;
  55.  
  56. GLint repeatCount = 1000;
  57. GLint loopCount = 100;
  58.  
  59. GLubyte texture[4*3] = {
  60.     0xFF, 0, 0, 0, 0, 0,
  61.     0, 0, 0, 0, 0xFF, 0,
  62. };
  63.  
  64.  
  65. static void SetWindSize(int width, int height)
  66. {
  67.  
  68.     windW = (GLint)width;
  69.     windH = (GLint)height;
  70. }
  71.  
  72. static GLenum Key(int key, GLenum mask)
  73. {
  74.  
  75.     switch (key) {
  76.       case TK_ESCAPE:
  77.     tkQuit();
  78.       case TK_a:
  79.     antialiasing = !antialiasing;
  80.     break;
  81.       case TK_d:
  82.     depthTesting = !depthTesting;
  83.     break;
  84.       case TK_f:
  85.     fogging = !fogging;
  86.     break;
  87.       case TK_F:
  88.     niceFogging = !niceFogging;
  89.     break;
  90.       case TK_s:
  91.     shading = !shading;
  92.     break;
  93.       case TK_t:
  94.     texturing = !texturing;
  95.     break;
  96.       default:
  97.     return GL_FALSE;
  98.     }
  99.     return GL_TRUE;
  100. }
  101.  
  102. static void Viewport(GLint row, GLint column)
  103. {
  104.     GLint x, y;
  105.  
  106.     boxW = (windW - (COLS + 1) * GAP) / COLS;
  107.     boxH = (windH - (ROWS + 1) * GAP) / ROWS;
  108.  
  109.     x = GAP + column * (boxW + GAP);
  110.     y = GAP + row * (boxH + GAP);
  111.  
  112.     glViewport(x, y, boxW, boxH);
  113.  
  114.     glMatrixMode(GL_PROJECTION);
  115.     glLoadIdentity();
  116.     gluOrtho2D(-boxW/2, boxW/2, -boxH/2, boxH/2);
  117.     glMatrixMode(GL_MODELVIEW);
  118.  
  119.    /* glEnable(GL_SCISSOR_TEST);*/
  120.     glScissor(x, y, boxW, boxH);
  121. }
  122.  
  123. static double Now(void)
  124. {
  125. #ifdef __unix
  126.     struct tms tm;
  127. #ifdef __MACHTEN__
  128.  
  129.     times(&tm);
  130.  
  131.     return (double)tm.tms_utime / (double)CLK_TCK;
  132. #else
  133.     clock_t clk;
  134.  
  135.     clk = times(&tm);
  136.  
  137. #ifdef CLK_TCK
  138.     return (double)clk / (double)CLK_TCK;
  139. #else
  140.     return (double)clk / (double)HZ;
  141. #endif
  142. #endif
  143.     return 0;
  144. #endif
  145. }
  146.  
  147. static void Report(const char *msg, float elapsed)
  148. {
  149.  
  150.     if (elapsed == 0.0) {
  151.     printf("%s per second: Unknown, elapsed time is zero\n", msg);
  152.     } else {
  153.     printf("%s per second: %g\n", msg, repeatCount*loopCount/elapsed);
  154.     }
  155. }
  156.  
  157. static void Points(void)
  158. {
  159.     GLint i, j;
  160.     float v1[3];
  161.     double start;
  162.  
  163.     start = Now();
  164.     for (i = 0; i < repeatCount; i++) {
  165.     v1[0] = 10;
  166.     v1[1] = 10;
  167.     v1[2] = 10;
  168.     glBegin(GL_POINTS);
  169.         for (j = 0; j < loopCount; j++) {
  170.         glVertex2fv(v1);
  171.         }
  172.     glEnd();
  173.     }
  174.     glFinish();
  175.     Report("Points", Now()-start);
  176. }
  177.  
  178. static void Lines(void)
  179. {
  180.     GLint i, j;
  181.     float v1[3], v2[3];
  182.     double start;
  183.  
  184.     start = Now();
  185.     for (i = 0; i < repeatCount; i++) {
  186.     v1[0] = 10;
  187.     v1[1] = 10;
  188.     v1[2] = 10;
  189.     v2[0] = 20;
  190.     v2[1] = 20;
  191.     v2[2] = 10;
  192.     glBegin(GL_LINES);
  193.         for (j = 0; j < loopCount; j++) {
  194.         glVertex2fv(v1);
  195.         glVertex2fv(v2);
  196.         }
  197.     glEnd();
  198.     }
  199.     glFinish();
  200.     Report("Lines", Now()-start);
  201. }
  202.  
  203. static void Triangles(void)
  204. {
  205.     GLint i, j;
  206.     float v1[3], v2[3], v3[3], t1[2], t2[2], t3[2];
  207.     double start;
  208.  
  209.     start = Now();
  210.  
  211.     v1[0] = 10;
  212.     v1[1] = 10;
  213.     v1[2] = 10;
  214.     v2[0] = 20;
  215.     v2[1] = 20;
  216.     v2[2] = 10;
  217.     v3[0] = 10;
  218.     v3[1] = 20;
  219.     v3[2] = 10;
  220.  
  221.     t1[0] = 0;
  222.     t1[1] = 0;
  223.     t2[0] = 1;
  224.     t2[1] = 1;
  225.     t3[0] = 0;
  226.     t3[1] = 1;
  227.  
  228.     for (i = 0; i < repeatCount; i++) {
  229.     glBegin(GL_TRIANGLES);
  230.         for (j = 0; j < loopCount; j++) {
  231.         if (texturing) {
  232.             glTexCoord2fv(t1);
  233.         }
  234.         glVertex2fv(v1);
  235.         if (texturing) {
  236.             glTexCoord2fv(t2);
  237.         }
  238.         glVertex2fv(v2);
  239.         if (texturing) {
  240.             glTexCoord2fv(t3);
  241.         }
  242.         glVertex2fv(v3);
  243.         }
  244.     glEnd();
  245.     }
  246.     glFinish();
  247.     Report("Triangles", Now()-start);
  248. }
  249.  
  250. static void Rects(void)
  251. {
  252.     GLint i, j;
  253.     float v1[2], v2[2];
  254.     double start;
  255.  
  256.     start = Now();
  257.     for (i = 0; i < repeatCount; i++) {
  258.     v1[0] = 10;
  259.     v1[1] = 10;
  260.     v2[0] = 20;
  261.     v2[1] = 20;
  262.     for (j = 0; j < loopCount; j++) {
  263.         glRectfv(v1, v2);
  264.     }
  265.     }
  266.     glFinish();
  267.     Report("Rects", Now()-start);
  268. }
  269.  
  270. static void Draw(void)
  271. {
  272.  
  273.     glClearColor(0.0, 0.0, 0.0, 0.0);
  274.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  275.  
  276.     TK_SETCOLOR(windType, TK_YELLOW);
  277.  
  278.     if (antialiasing) {
  279.     glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  280.     glEnable(GL_BLEND);
  281.  
  282.     glEnable(GL_POINT_SMOOTH);
  283.     glEnable(GL_LINE_SMOOTH);
  284.     glEnable(GL_POLYGON_SMOOTH);
  285.     printf("antialiasing: on\n");
  286.     }
  287.     else {
  288.        glDisable(GL_BLEND);
  289.        glDisable(GL_POINT_SMOOTH);
  290.        glDisable(GL_LINE_SMOOTH);
  291.        glDisable(GL_POLYGON_SMOOTH);
  292.        printf("antialiasing: off\n");
  293.     }
  294.     if (depthTesting) {
  295.     glEnable(GL_DEPTH_TEST);
  296.     printf("depthtest: on\n");
  297.     }
  298.     else {
  299.        glDisable(GL_DEPTH_TEST);
  300.        printf("depthtest: off\n");
  301.     }
  302.     if (fogging) {
  303.     glEnable(GL_FOG);
  304.     glHint(GL_FOG_HINT, (niceFogging) ? GL_NICEST : GL_FASTEST);
  305.     printf("fog: on\n");
  306.     }
  307.     else {
  308.        glDisable(GL_FOG);
  309.        printf("fog: off\n");
  310.     }
  311.     if (lighting) {
  312.     static GLfloat ambient[4] = {1, 0.5, 0.5, 0};
  313.  
  314.     glEnable(GL_NORMALIZE);
  315.     glNormal3f(1.0, 1.0, 1.0);
  316.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
  317.     glEnable(GL_LIGHTING);
  318.     glEnable(GL_LIGHT0);
  319.     printf("lighting: on\n");
  320.     }
  321.     else {
  322.        glDisable( GL_LIGHTING );
  323.        printf("lighting: off\n");
  324.      }
  325.     if (shading) {
  326.        glShadeModel(GL_SMOOTH);
  327.        printf("shading: smooth\n");
  328.     }
  329.     else {
  330.        glShadeModel(GL_FLAT);
  331.        printf("shading: flat\n");
  332.     }
  333.     if (texturing) {
  334.     static GLfloat modulate[1] = {GL_DECAL};
  335.     static GLfloat clamp[1] = {GL_CLAMP};
  336.     static GLfloat linear[1] = {GL_LINEAR};
  337.  
  338.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  339.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE,
  340.              (GLvoid *)texture);
  341.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modulate);
  342.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
  343.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
  344.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear);
  345.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear);
  346.     glEnable(GL_TEXTURE_2D);
  347.     printf("texturing: on\n");
  348.     }
  349.     else {
  350.        glDisable( GL_TEXTURE_2D );
  351.        printf("texturing: off\n");
  352.      }
  353.  
  354.     Viewport(0, 0); Points();
  355.     Viewport(0, 1); Lines();
  356.     Viewport(0, 2); Triangles();
  357.     Viewport(0, 3); Rects();
  358.  
  359.     glFlush();
  360.  
  361.     if (doubleBuffer) {
  362.     tkSwapBuffers();
  363.     }
  364. }
  365.  
  366. static GLenum Args(int argc, char **argv)
  367. {
  368.     GLint i;
  369.  
  370.     rgb = GL_TRUE;
  371.     doubleBuffer = GL_FALSE;
  372.     directRender = GL_TRUE;
  373.  
  374.     for (i = 1; i < argc; i++) {
  375.     if (strcmp(argv[i], "-ci") == 0) {
  376.         rgb = GL_FALSE;
  377.     } else if (strcmp(argv[i], "-rgb") == 0) {
  378.         rgb = GL_TRUE;
  379.     } else if (strcmp(argv[i], "-sb") == 0) {
  380.         doubleBuffer = GL_FALSE;
  381.     } else if (strcmp(argv[i], "-db") == 0) {
  382.         doubleBuffer = GL_TRUE;
  383.     } else if (strcmp(argv[i], "-dr") == 0) {
  384.         directRender = GL_TRUE;
  385.     } else if (strcmp(argv[i], "-ir") == 0) {
  386.         directRender = GL_FALSE;
  387.     } else {
  388.         printf("%s (Bad option).\n", argv[i]);
  389.         return GL_FALSE;
  390.     }
  391.     }
  392.     return GL_TRUE;
  393. }
  394.  
  395. void main(int argc, char **argv)
  396. {
  397.  
  398.     if (Args(argc, argv) == GL_FALSE) {
  399.     tkQuit();
  400.     }
  401.  
  402.     windW = 600;
  403.     windH = 300;
  404.     tkInitPosition(0, 0, windW, windH);
  405.  
  406.     windType = TK_ALPHA | TK_DEPTH;
  407.     windType |= (rgb) ? TK_RGB : TK_INDEX;
  408.     windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  409.     windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  410.     tkInitDisplayMode(windType);
  411.  
  412.     if (tkInitWindow("Speed Test") == GL_FALSE) {
  413.     tkQuit();
  414.     }
  415.  
  416.     tkExposeFunc(SetWindSize);
  417.     tkReshapeFunc(SetWindSize);
  418.     tkKeyDownFunc(Key);
  419.     tkDisplayFunc(Draw);
  420.     tkExec();
  421. }
  422.